package com.zhw.controller;

import com.zhw.Util.AliOSSUtils;
import com.zhw.anno.Log;
import com.zhw.pojo.Emp;
import com.zhw.pojo.PageBean;
import com.zhw.pojo.Result;
import com.zhw.service.EmpService;
import com.zhw.service.impl.EmpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;

@RestController
public class EmpController {

    @Autowired
    EmpService esi;


    @Autowired
    AliOSSUtils aos;


    //    获取所有的员工信息、当前页码以及页码每行的数据量
    @GetMapping("/emps")
    public Result getAll(@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer pageSize,
                         String name, Integer gender,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ) {

        PageBean pb = esi.getAll(page, pageSize, name, gender, begin, end);
        return Result.success(pb);
    }


    //    删除某个员工
    @Log
    @DeleteMapping("/emps/{ids}")
    public Result delete(@PathVariable int[] ids) {
        esi.delete(ids);
        return Result.success();
    }


    //    添加员工信息
    @Log
    @PostMapping("/emps")
    public Result insert(@RequestBody Emp emp) {
        esi.insert(emp);
        return Result.success();
    }


    //上传文件
    @Log
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        String url = aos.upload(image);
        return Result.success(url);
    }


    //    根据ID查询员工信息
    @GetMapping("/emps/{id}")
    public Result selectById(@PathVariable Integer id) {
        Emp emp = esi.selectById(id);
        return Result.success(emp);

    }


    //    修改员工信息
    @Log
    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp) {
        esi.update(emp);
        return Result.success();
    }


}
